package com.orange.homescreen.pil.defaultimpl.NotificationSummary;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;

import com.orange.homescreen.pil.base.BasePILMonitor;
import com.orange.notificationsummary.utils.LogHelper;

public class NotificationVoicemailMonitor extends BasePILMonitor {

	private static final String TAG = "VIPContactVVMMonitor";

	public static final Uri	VVM_URI				= Uri.parse("content://com.orange.vvm.service.provider/message");

	private boolean			mFlag				= false;
	private ContentResolver	mContactsResolver	= null;
	private ContentObserver	mContactsObserver	= null;

	public NotificationVoicemailMonitor(Context context) {
		super(context);
	}

	@Override
	public void onPause() {
	}

	@Override
	public void onResume() {
	}

	@Override
	public boolean onStart() {

		// if (mFlag == false) {
		if (!mFlag) {
			mContactsResolver = mContext.getContentResolver();
			mContactsObserver = new ContentObserver(new Handler()) {

				@Override
				public boolean deliverSelfNotifications() {
					return super.deliverSelfNotifications();
				}

				@Override
				public void onChange(boolean selfChange) {
					notifyListeners();
				}

			};
			try {
				mContactsResolver.registerContentObserver(VVM_URI, true,
						mContactsObserver);
			} catch (Exception e){
				// Ignore, when there is no VVM.
				LogHelper.e(TAG, "Failed to register.", e);
			}
			mFlag = true;
		}
		return true;
	}

	@Override
	public void onStop() {
		if (mContactsResolver != null && mContactsObserver != null) {
			try {
				mContactsResolver.unregisterContentObserver(mContactsObserver);
				
			} catch (Exception e) {
				// Ignore, when there is no VVM.
				LogHelper.e(TAG, "Failed to unregister.", e);
			}
		}
		mContactsResolver = null;
		mContactsObserver = null;
		mFlag = false;
	}
}
